import { request } from '@/utils/request'

enum API {
  USER_LIST_URL = '/api/sys/user/list',
  USER_DELETE_URL = '/api/sys/user/delete',
  USER_INFO_URL = '/api/sys/user/info',
  USER_UPDATE_COMMIT = 'api/sys/user',

  ROLE_SELECT_URL = '/api/sys/role/select',
  ROLE_LIST_URL = '/api/sys/role/list',
  ROLE_INFO_URL = '/api/sys/role/info',
  ROLE_DELETE_URL = '/api/sys/role/delete',
  ROLE_UPDATE_COMMIT = '/api/sys/role',

  MENU_LIST_URL = '/api/sys/menu/list',
  MENU_SELECT_URL = '/api/sys/menu/select',
  MENU_INFO_URL = '/api/sys/menu/info',
  MENU_DELETE_URL = '/api/sys/menu/delete',
  MENU_UPDATE_COMMIT = '/api/sys/menu',
}

export const reqUserList = (page: number, limit: number, username: string) =>
  request.get<any, any>(
    API.USER_LIST_URL + `?page=${page}&limit=${limit}&username=${username}`,
  )

export const reqUserDelete = (id: any) =>
  request.post<any, any>(API.USER_DELETE_URL, id)

export const reqUserInfo = (id: any) =>
  request.get<any, any>(API.USER_INFO_URL + `/${id}`)

export const reqSelectInfo = () => request.get<any, any>(API.ROLE_SELECT_URL)

export const reqRoleList = (page: number, limit: number, username: string) =>
  request.get<any, any>(
    API.ROLE_LIST_URL + `?page=${page}&limit=${limit}&username=${username}`,
  )

export const reqRoleInfo = (id: any) =>
  request.get<any, any>(API.ROLE_INFO_URL + `/${id}`)

export const reqRoleDelete = (id: any) =>
  request.post<any, any>(API.ROLE_DELETE_URL, id)

export const reqMenuList = () => request.get<any, any>(API.MENU_LIST_URL)

export const reqMenuDelete = (id: any) =>
  request.post<any, any>(API.MENU_DELETE_URL + `/${id}`)

export const reqMenuSelectInfo = () =>
  request.get<any, any>(API.MENU_SELECT_URL)

export const reqMenuInfo = (id: any) =>
  request.get<any, any>(API.MENU_INFO_URL + `/${id}`)

export const reqUserUpdateCommit = (
  type: string,
  roleId: number,
  username: string,
  password: string,
  email: string,
  mobile: string,
  salt: string,
  status: number,
  roleIdList: string[],
) => {
  if (type == 'save') {
    return request.post<any, any>(API.USER_UPDATE_COMMIT + `/${type}`, {
      username,
      password,
      email,
      mobile,
      salt,
      status,
      roleIdList,
    })
  } else {
    return request.post<any, any>(API.USER_UPDATE_COMMIT + `/${type}`, {
      roleId,
      username,
      password,
      email,
      mobile,
      salt,
      status,
      roleIdList,
    })
  }
}

export const reqRoleUpdateCommit = (
  type: string,
  roleId: number,
  roleName: string,
  remark: string,
  menuIdList: string[],
) => {
  if (type == 'save') {
    return request.post<any, any>(API.ROLE_UPDATE_COMMIT + `/${type}`, {
      roleName,
      remark,
      menuIdList,
    })
  } else {
    return request.post<any, any>(API.ROLE_UPDATE_COMMIT + `/${type}`, {
      roleId,
      roleName,
      remark,
      menuIdList,
    })
  }
}

export const reqMenuUpdateCommit = (
  typeUp: string,
  id: number,
  type: number,
  name: string,
  parentId: number,
  url: string,
  perms: string,
  icon: string,
  orderNum: number,
  roleIdList: string[],
) => {
  if (typeUp == 'save') {
    return request.post<any, any>(API.MENU_UPDATE_COMMIT + `/${typeUp}`, {
      type,
      name,
      parentId,
      url,
      perms,
      icon,
      orderNum,
      roleIdList,
    })
  } else {
    return request.post<any, any>(API.MENU_UPDATE_COMMIT + `/${typeUp}`, {
      id,
      type,
      name,
      parentId,
      url,
      perms,
      icon,
      orderNum,
      roleIdList,
    })
  }
}
